package com.example.springevent.observer;

import java.math.BigDecimal;
import java.util.Observable;
import java.util.Observer;

import org.springframework.beans.BeanUtils;

import com.example.springevent.event.bo.IvdAddedEventBO;
import com.example.springevent.event.bo.IvdRemovedEventBO;
import com.example.springevent.ivd.domain.InvoiceVerifyDetail;

/**
 * 
 * @author jie.deng
 * @see java.util.Observable
 * @see java.util.Observer
 */
public class ObservableDemo {

	public static void main(String[] args) {
		ObservableDemo demo = new ObservableDemo();
		demo.testObservable();
		demo.testCustomizedObservable();
	}

	public void testCustomizedObservable() {
		IvdChangedObservable observable = new IvdChangedObservable();
		// 注册观察者
		observable.addObserver(new IvdChangedObserverInvoiceHandler());
		observable.addObserver(new IvdChangedObserverPoHandler());

		// ivd业务逻辑。。。
		InvoiceVerifyDetail ivd = new InvoiceVerifyDetail(6L, new BigDecimal(32), "Po005", 1L, "Code001", "No005", 1L, "A");

		// 组装BO，通知观察者
		IvdAddedEventBO addedEventBO = new IvdAddedEventBO();
		IvdRemovedEventBO removedEventBO = new IvdRemovedEventBO();
		BeanUtils.copyProperties(ivd, addedEventBO);
		BeanUtils.copyProperties(ivd, removedEventBO);
		observable.notifyObserversIvdAdded(addedEventBO);
		observable.notifyObserversIvdRemoved(removedEventBO);

	}

	public void testObservable() {
		Observable observable = new Observable();
		// 注册观察者
		observable.addObserver(new Observer() {

			@Override
			public void update(Observable o, Object arg) {
				System.out.printf("Observable={%s}, arg= {%s}", o, arg);
			}

		});

		observable.notifyObservers("hello world");

	}

}
